﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using System.Data.SqlClient;
using System.Configuration;


namespace syc.Fc
{
    public partial class 备件到货发货管理 : System.Web.UI.Page
    {
        string sql =
@"SELECT t1.ID,t2.品名,t2.规格,t2.单位,t1.数量,t1.结存,cast(t1.单价 as float) as 单价
,isnull(t1.厂商,'') as 厂商,t1.日期,case when 数量<0 then '发货' else '到货' end as 到发,t3.名称 as 部门
FROM 备件库存 t1 left outer join 备品备件 t2 on t2.ID=t1.类别ID left outer join 设备编码 t3 on t3.编码=t1.部门 and t3.资产=0";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                this.BuMen.DataBind();
                this.BuMen.Enabled = false;
                if ((object)Session["OperatorDCode"] != null)
                {
                    string dcode = (string)Session["OperatorDCode"];
                    if (this.BuMen.Items.FindByValue(dcode) != null)
                    {
                        this.BuMen.Text = dcode;
                    }
                }
                this.Button_Query_Click(null, null);
            }
            else
            {
                this.SqlDataSource1.SelectCommand = this.SqlText.Text;
            }
        }

        protected void GridView1_DataBound(object sender, EventArgs e)
        {
            this.GridView1.SelectedIndex = -1;
            this.Button_View.Enabled = false;
            this.Button_Edit.Enabled = false;
            this.Button_Delete.Enabled = false;

            this.TextBox_Pager.Text = (this.GridView1.PageIndex + 1).ToString();
            this.Label_Pages.Text = " 共" + this.GridView1.PageCount.ToString() + "页 ";

            this.Button_Pager_First.CssClass = "pageturn-first-b";
            this.Button_Pager_First.Enabled = true;
            this.Button_Pager_Prev.CssClass = "pageturn-prev-b";
            this.Button_Pager_Prev.Enabled = true;
            this.Button_Pager_Next.CssClass = "pageturn-next-b";
            this.Button_Pager_Next.Enabled = true;
            this.Button_Pager_Last.CssClass = "pageturn-last-b";
            this.Button_Pager_Last.Enabled = true;
            if (this.GridView1.PageIndex == 0)
            {
                this.Button_Pager_Prev.CssClass = "pageturn-prev-g";
                this.Button_Pager_Prev.Enabled = false;
            }
            if (this.GridView1.PageIndex < 2)
            {
                this.Button_Pager_First.CssClass = "pageturn-first-g";
                this.Button_Pager_First.Enabled = false;
            }
            if (this.GridView1.PageIndex == this.GridView1.PageCount - 1 || this.GridView1.PageCount == 0)
            {
                this.Button_Pager_Next.CssClass = "pageturn-next-g";
                this.Button_Pager_Next.Enabled = false;

            }
            if (this.GridView1.PageIndex > this.GridView1.PageCount - 3)
            {
                this.Button_Pager_Last.CssClass = "pageturn-last-g";
                this.Button_Pager_Last.Enabled = false;
            }

        }
        protected void GridView1_PageIndexChanged(object sender, EventArgs e)
        {
            this.GridView1_DataBound(null, null);
        }
        protected void Pager_Click(object sender, EventArgs e)
        {
            if (sender.GetType().ToString() == "System.Web.UI.WebControls.Button")
            {
                switch (((Button)sender).CommandName)
                {
                    case "--":
                        this.GridView1.PageIndex = 0;
                        break;
                    case "-":
                        if (this.GridView1.PageIndex > 0) this.GridView1.PageIndex--;
                        break;
                    case "+":
                        if (this.GridView1.PageIndex < this.GridView1.PageCount - 1) this.GridView1.PageIndex++;
                        break;
                    case "++":
                        this.GridView1.PageIndex = this.GridView1.PageCount - 1;
                        break;
                }
            }
            else
            {

                int p;
                if (int.TryParse(TextBox_Pager.Text, out p))
                {
                    if (p >= 1 && p <= this.GridView1.PageCount)
                    {
                        this.GridView1.PageIndex = p - 1;
                    }
                }
                TextBox_Pager.Text = (this.GridView1.PageIndex + 1).ToString();
            }

        }
        protected void SqlDataSource1_Selected(object sender, SqlDataSourceStatusEventArgs e)
        {
            Label_Total.Text = "共查询到 " + e.AffectedRows + " 项记录";
        }
        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.SelectedID.Text = this.GridView1.SelectedDataKey[0].ToString();
            this.Button_View.Enabled = true;
            this.Button_View.OnClientClick = "parent.swin2_load('/Fc/备件到货发货登记.aspx?id=" + this.SelectedID.Text + "',760,600,'yes');return false;";

            this.Button_Edit.Enabled = true;
            this.Button_Edit.OnClientClick = "parent.swin2_load('/Fc/备件到货发货登记.aspx?id=" + this.SelectedID.Text + "',600,350);return false;";

            this.Button_Delete.Enabled = true;
        }
        private string GetState()
        {
            string state = "";


            if (this.BuMen.Text != "35")
            {
                state += " and t1.部门 ='"+this.BuMen.Text+"'";
            }

            if (this.PinMing.Text != "")
            {
                state += " and t2.品名 like '%" + this.PinMing.Text + "%'";
            }
            if (this.ChangShang.Text != "")
            {
                state += " and t1.厂商 like '%" + this.ChangShang.Text + "%'";
            }

            DateTime d;
            if (DateTime.TryParse(this.Date1.Text, out d))
            {
                state += " and t1.日期>='" + d.ToShortDateString() + "'";
            }
            if (DateTime.TryParse(this.Date2.Text, out d))
            {
                state += " and t1.日期<='" + d.ToShortDateString() + "'";
            }

            if (state.StartsWith(" and"))
            {
                state = state.Substring(4);
            }
            if (state != "")
            {
                state = " where " + state;
            }
            return state;
        }
        protected void Button_Query_Click(object sender, EventArgs e)
        {
            string state = this.GetState();
            this.SqlText.Text = sql + state + " order by ID desc";
            this.GridView1.Sort("ID", SortDirection.Descending);
            this.SqlDataSource1.SelectCommand = "";
            this.SqlDataSource1.SelectCommand = this.SqlText.Text;
        }

        protected void Button_Reset_Click(object sender, EventArgs e)
        {
            this.PinMing.Text = "";
            this.ChangShang.Text = "";
            this.Date1.Text = "";
            this.Date2.Text = "";
        }

        protected void Button_Delete_Click(object sender, EventArgs e)
        {
            this.SqlDataSource1.DeleteParameters["ID"].DefaultValue = this.SelectedID.Text;
            if (this.SqlDataSource1.Delete() > 0)
            {
                //this.Message.Text = "·保存成功";
            }
            else
            {
                //this.Message.Text = "·保存失败";
            }
        }

        protected void Button_Export_Click(object sender, EventArgs e)
        {

            string part1 =
@"<html xmlns:o='urn:schemas-microsoft-com:office:office'
xmlns:x='urn:schemas-microsoft-com:office:excel'
xmlns='http://www.w3.org/TR/REC-html40'>
<head>
<meta http-equiv=Content-Type content='text/html; charset=utf-8'>
<style>
<!--table
@page
{mso-header-data:;
mso-footer-data:;
margin:.79in .31in .79in .31in;
mso-header-margin:.51in;
mso-footer-margin:.51in;
mso-page-orientation:landscape;
mso-horizontal-page-align:center;
}
-->
</style>
<!--[if gte mso 9]><xml>
 <x:ExcelWorkbook>
  <x:ExcelWorksheets>
   <x:ExcelWorksheet>
    <x:Name>备品备件到货发货明细</x:Name>
    <x:WorksheetOptions>
     <x:DefaultRowHeight>285</x:DefaultRowHeight>
     <x:Print>
      <x:ValidPrinterInfo/>
      <x:Scale>100</x:Scale>
      <x:PaperSizeIndex>9</x:PaperSizeIndex>
      <x:HorizontalResolution>200</x:HorizontalResolution>
      <x:VerticalResolution>200</x:VerticalResolution>
     </x:Print>
     <x:Selected/>
     <x:Panes>
      <x:Pane>
       <x:Number>3</x:Number>
       <x:ActiveRow>0</x:ActiveRow>
       <x:ActiveCol>0</x:ActiveCol>
      </x:Pane>
     </x:Panes>
    </x:WorksheetOptions>
   </x:ExcelWorksheet>
 </x:ExcelWorkbook>
</xml><![endif]-->
</head>
<body>";
            string part2 =
@"<style>
tr{height:24px;}
td{text-align:center;font-size:10pt;}
td.s1{border:.5pt solid #000000;border-top:1pt solid #000000;border-left:1pt solid #000000;}
td.s2{border:.5pt solid #000000;border-top:1pt solid #000000;}
td.s3{border:.5pt solid #000000;border-top:1pt solid #000000;border-right:1pt solid #000000;}
td.s4{border:.5pt solid #000000;border-left:1pt solid #000000;}
td.s5{border:.5pt solid #000000;}
td.s6{border:.5pt solid #000000;border-right:1pt solid #000000;}
td.s7{border:.5pt solid #000000;border-bottom:1pt solid #000000;border-left:1pt solid #000000;}
td.s8{border:.5pt solid #000000;border-bottom:1pt solid #000000;}
td.s9{border:.5pt solid #000000;border-bottom:1pt solid #000000;border-right:1pt solid #000000;}
</style>
<table border=0 cellpadding=0 style=border-collapse:collapse>
<tr>
<td height=40 colspan=10 style=font-size:18pt;><b><u>&nbsp;备品备件到货发货明细&nbsp;</u></b></td>
</tr>";

string part3 =
@"<tr>
<td class=s1 width=60>到发</td>
<td class=s2 width=120>部门</td>
<td class=s2 width=120>品名</td>
<td class=s2 width=120>规格</td>
<td class=s2 width=60>单位</td>
<td class=s2 width=120>厂商</td>
<td class=s2 width=70>数量</td>
<td class=s2 width=80>单价(元)</td>
<td class=s2 width=70>结存</td>
<td class=s3 width=90>日期</td>
</tr>";
            string part4 =
@"
</table>
</body>
</html>";


            Response.Charset = "utf-8";
            Response.ContentEncoding = System.Text.Encoding.UTF8;
            Response.ContentType = "application/ms-excel";
            Response.AddHeader("content-disposition", "attachment; filename=\"" + Server.UrlEncode("备品备件到货发货明细") + ".xls\"");

            Response.Write(part1);
            Response.Write(part2);
            Response.Write(part3);


            SqlConnection Conn = new SqlConnection();
            Conn.ConnectionString = ConfigurationManager.ConnectionStrings["SCMConnectionString1"].ConnectionString;
            Conn.Open();
            SqlCommand Cmd = new SqlCommand();
            Cmd.Connection = Conn;

            string state = this.GetState();
            Cmd.CommandText = sql + state + " order by ID";

            Cmd.CommandType = CommandType.Text;
            SqlDataReader Reader = Cmd.ExecuteReader();


            while (Reader.Read())
            {
                Response.Write("<tr><td class=s4>"+(string)Reader["到发"]+"</td>");
                Response.Write("<td class=s5>"+(string)Reader["部门"]+"</td>");
                Response.Write("<td class=s5>"+(string)Reader["品名"]+"</td>");
                Response.Write("<td class=s5>"+(string)Reader["规格"]+"</td>");
                Response.Write("<td class=s5>"+(string)Reader["单位"]+"</td>");
                Response.Write("<td class=s5>"+(string)Reader["厂商"]+"</td>");
                Response.Write("<td class=s5>"+(int)Reader["数量"]+"</td>");
                Response.Write("<td class=s5>"+(double)Reader["单价"]+"</td>");
                Response.Write("<td class=s5>"+(int)Reader["结存"]+"</td>");
                Response.Write("<td class=s6>"+((DateTime)Reader["日期"]).ToShortDateString()+"</td></tr>");
            }

            Response.Write(
@"<tr><td class=s7></td>
<td class=s8></td>
<td class=s8></td>
<td class=s8></td>
<td class=s8></td>
<td class=s8></td>
<td class=s8></td>
<td class=s8></td>
<td class=s8></td>
<td class=s9></td></tr>");
            Reader.Close();
            Conn.Close();
            Reader.Dispose();
            Cmd.Dispose();
            Conn.Dispose();

            Response.Write(part4);
            Response.End();
        }
    }
}
